﻿package game.objects {
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import game.events.GameEvent;
	import game.value.GameValues;
	
	public class Cockroach extends MovieClip {
		
		protected var speedX:int = 2;
		protected var speedY:int = 2;
		protected const foodNumber:int = 2;
		protected var locationType:int = 0;
		
		private const first:int = 540;
		private const second:int = 337;
		private const third:int = 156;
		private const starX:int = 1015;
		private const stopLocationX:int = 150;
				
		protected var speedScale:int = 2;
		public var moveAble:Boolean = true;
		
		public var game:main;
		public var pudding:Pudding;//布丁物件
		public var Hittag:Boolean = false;//碰撞tag
		
		
		public function Cockroach(mygame:main) {
			this.game = mygame;
			
			speedX = speedX * this.speedScale;
			speedY = speedY * this.speedScale;
			
			this.addEventListener(Event.ENTER_FRAME, EnterFrameHandler);
			this.addEventListener(GameEvent.ENEMY_ARRIVE, EnemyArriveHandler);
			this.addEventListener(GameEvent.ENEMY_DEAD, EnemyDeadHandler);
		}
		
		
		
		//初始位置設定
		public function setLocation(location:int):void {
			locationType = location;
			switch(locationType) {
				case 0:
					this.x = this.starX;
					this.y = this.first;
					break;
				case 1:
					this.x = this.starX;
					this.y = this.second;
					break;
				case 2:
					this.x = this.starX;
					this.y = this.third;
					break;
				default:
					this.x = this.starX;
					this.y = this.first;
					break;
			}
		}
		
		//移動控制
		public function EnterFrameHandler(e:Event):void {
			if (moveAble)
			{
				this.x = this.x - speedX;
				//this.y = this.y + speedY;
				this.FlowerHitHandler();
				
				if (this.x < this.stopLocationX)
				{
					moveAble = false;
				}
			}
			else if (!moveAble)
			{
				trace(this.dispatchEvent(new Event(GameEvent.ENEMY_ARRIVE, false, false)));
				this.removeEventListener(Event.ENTER_FRAME, EnterFrameHandler);
			}
			if (!game.gameValue.showBugs)
			{
				trace(this.dispatchEvent(new Event(GameEvent.ENEMY_DEAD, false, false)));
				this.removeEventListener(Event.ENTER_FRAME, EnterFrameHandler);
			}
		}
		
		//到達食物區
		public function EnemyArriveHandler(e:Event):void {
			trace("EnemyArrifeHandler");
			this.addEventListener(Event.ENTER_FRAME, EnemyShadeAnimation);
			
			//移除食物元件
			removeFood();
		}
		
		//碰撞控制
		private function FlowerHitHandler():void 
		{
			if (this.hitTestObject(game.flower.collisionRec))
			{
				trace("hit");
				//moveAble = false;
				trace(this.dispatchEvent(new Event(GameEvent.ENEMY_DEAD, false, false)));
				this.removeEventListener(Event.ENTER_FRAME, EnterFrameHandler);
			}
		}
		
		//退出動畫
		public function EnemyShadeAnimation(e:Event):void {
			if (this.alpha > 0)
			{
				this.alpha -= 0.07;
			}
			else
			{
				this.removeEventListener(Event.ENTER_FRAME, EnemyShadeAnimation);
				trace(this.dispatchEvent(new Event(GameEvent.ENEMY_DEAD, false, false)));
			}
		}
		
		//移除物件
		public function EnemyDeadHandler(e:Event):void {
			
			trace("EnemyDeadHandler");
			parent.removeChild(this);
			
		}
		
		//移除食物元件判斷
		public function removeFood():void {
			switch (this.y) 
			{
				case this.first:
					game.pudding.reduceNum(foodNumber);
				break;
				case this.second:
					game.chicken.reduceNum(foodNumber);
				break;
				case this.third:
					game.drink.reduceNum(foodNumber);
				break;
				default:
			}
		}
	}
	
}
